package AS_source
{
	import com.adobe.utils.XMLUtil;
	import com.adobe.xml.syndication.rss.Item20;
	import com.adobe.xml.syndication.rss.RSS20;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	
	import mx.controls.Alert;
/*
*Everything is done as far as RSS automatically just call getSchoolXML() to get the concentrations in one 
*nice and neat little package. 
*/
	public class SpecRank
	{
		var rankHelper:RankSearch;
		public function SpecRank()
		{
			this.getConcentrations();
			//helper.setSchoolXML;	
		}
		private var loader:URLLoader;

//url of rss 2.0 feed
		private static const RSS_URL1:String = "http://pipes.yahoo.com/pipes/pipe.run?_id=0235615a1953b1385cda27b88ee3491c&_render=rss"; 
		private var specialties:String; 
		private var schoolXML:XML;
		private function getSpecialties():String{
			return specialties; 
		}
		private function setSpecialties(val:String):void{
			specialties = val; 
		}
//called when user presses the button to load feed
		public function getConcentrations():void
		{
			Alert.show("FIrst");
			loadRSSToXML(RSS_URL1);//LEAVE ALONE

		}
		private function loadRSSToXML(URL:String){
			loader = new URLLoader();
			//request pointing to feed
			var request:URLRequest = new URLRequest(URL);
			request.method = URLRequestMethod.GET;
			//listen for when the data loads
			loader.addEventListener(Event.COMPLETE, onDataLoad);
			//listen for error events
			loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
			//load the feed data
			loader.load(request);	

		}
//called once the data has loaded from the feed
		private function onDataLoad(e:Event):void
		{
			//get the raw string data from the feed
			var rawRSS:String = URLLoader(e.target).data;
			//parse it as RSS
			parseRSS(rawRSS);

		}

//parses RSS 2.0 feed and prints out the feed titles into
//the text area
		private function parseRSS(data:String):void
		{
			//XMLSyndicationLibrary does not validate that the data contains valid
			//XML, so you need to validate that the data is valid XML.
			//We use the XMLUtil.isValidXML API from the corelib library.
			var schoolPrograms:String = "";
			if(!XMLUtil.isValidXML(data))
			{
				Alert.show("Feed does not contain valid XML.");
				return;
			}	
			//create RSS20 instance
			var rss:RSS20 = new RSS20();

			//parse the raw rss data
			rss.parse(data);

			//get all of the items within the feed
			var items:Array = rss.items;

			//loop through each item in the feed
			for each(var item:Item20 in items)
			{
				schoolPrograms += item.title		
			}
			var schoolStr:String = "<programList>  \n" + schoolPrograms 
				+ " \n </programList>";
			setSchoolXML(new XML(schoolStr));
			//Alert.show(schoolXML);
		}
		public function getSchoolXML():XML{
			return schoolXML;
		}
		private function setSchoolXML(data:XML){
			schoolXML = data;
			//set the RankSearch's XML
			Alert.show("From SpecRanksSET: " + schoolXML);
		}
		private function onIOError(e:IOErrorEvent):void
		{
			Alert.show("IOError : " + e.text);
		}

		private function onSecurityError(e:SecurityErrorEvent):void
		{
			Alert.show("SecurityError : " + e.text);
		}
	}
}
